
#include <fstream>
#include <csignal>
#include <iostream>
#include "rutil/Time.hxx"
#include "rutil/Random.hxx"

#include "runner.h"
#include "inifile.h"

using namespace inifile;
using namespace std;
using namespace resip;

static bool finished = false;
static void signal_handler(int signo)
{
    cerr << "Shutting down..." << endl;
    finished = true;
}

int main(int argc, char* argv[])
{
    if (signal(SIGINT, signal_handler) == SIG_ERR)
    {
        cerr << "Couldn't install signal handler for SIGINT" << endl;
        exit(-1);
    }

    if (signal(SIGTERM, signal_handler) == SIG_ERR)
    {
        cerr << "Couldn't install signal handler for SIGTERM" << endl;
        exit(-1);
    }

    common::helper::NetworkInitializer net;
    if (!net) {
        cerr << "Couldn't initialize network" << endl;
        return 0;
    }

    ConfigParam config;
    try
    {
        if (argc < 2)
        {
            config.load_config();
        }
        else
        {
            config.load_config(argv[1]);
        }
    }
    catch (std::exception& e)
    {
        cerr << e.what() << endl;
        return 0;
    }

    Runner runner(config);
    if (!runner.run())
    {
        cout << "run failed!" << endl;
        return 0;
    }

    int id = 0x1000;
    while (!finished)
    {
        string cmd;
        std::getline(cin, cmd);
        if (cmd == "reg")
        {
            runner.doRegister();
        }
        else if (cmd.substr(0, 4) == "call")
        {
            string target;
            if (cmd.size() > 5)
                target = cmd.substr(5);
            if (target.empty())
                target = "conf=1001";
            runner.send_call(target);
        }
        else if (cmd.substr(0, 4) == "info")
        {
            string info;
            if (cmd.size() > 5)
                info = cmd.substr(5);
            if (info.empty())
                info = "[[[zk_mouse_event_broadcast: mouse moveto [100,200]; mouse click left]]]";
            runner.send_info(info, (void*)id++);
        }
        else if (cmd.substr(0, 6) == "hangup")
        {
            runner.terminate_call("Manaual hangup.");
        }
        else if (cmd.substr(0, 6) == "reload")
        {
            string newFilepath;
            if (cmd.size() > 7)
                newFilepath = cmd.substr(7);
            if (newFilepath.empty())
                newFilepath = "./config.ini";

            ConfigParam newConfig;
            try
            {
                newConfig.load_config(newFilepath);
            }
            catch (std::exception& e)
            {
                cerr << "configuration file reload err:" << e.what() << endl;
                return 0;
            }

            runner.stop();
            runner.uninit();
            runner.param() = newConfig;
            if (!runner.run())
            {
                cout << "run failed!" << endl;
                return 0;
            }

            cout << "reload done." << endl;
        }
        else if (cmd.substr(0, 7) == "console")
        {
            string level;
            if (cmd.size() > 8)
                level = cmd.substr(8);
            if (!level.empty())
                runner.set_console_level(level);
        }
        else if (cmd == "q")
        {
            break;
        }
        else
        {
            cout<< "reg                 send register" << endl
                << "call [target]       send call to target [default is 'conf=1001'], sdp from file `./sdp.txt`" << endl
                << "hangup              hang up exist call" << endl
                << "info [message]      send info wihtin dialog created by call [default message is zk_mouse_event_broadcast]" << endl
                //<< "reload [filepath]   reload from file config.ini [default is config.ini in current work directory]" << endl
                << "console [level]     set the log filtering level of the console [trace | debug | info | warning | error | critical | off]" << endl
                << "q                   exit" << endl << endl;
        }
    }

//     while (!finished)
//     {
//         common::helper::msleep(100);
//     }

    return 0;
}




